import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef CommentCallback = void Function(String content);

class CommentInputWidget extends StatefulWidget {
  final CommentCallback onTapComment;
  final FocusNode commentFocus;
  final String hintText;
  final String btnText;

  CommentInputWidget({this.onTapComment, this.commentFocus, this.hintText, this.btnText = '发送'});

  @override
  _CommentInputWidgetState createState() => _CommentInputWidgetState();
}

class _CommentInputWidgetState extends State<CommentInputWidget> {
  final TextEditingController _editingController = TextEditingController();

  bool _isClick = false;

  @override
  void initState() {
    _editingController.addListener(_verify);
    super.initState();
  }

  void _verify() {
    String text = _editingController.text;
    bool isClick = true;
    if (text.isEmpty) {
      isClick = false;
    }

    /// 状态不一样在刷新，避免重复不必要的setState
    if (isClick != _isClick) {
      setState(() {
        _isClick = isClick;
      });
    }
  }

  void sendComment() {
    String text = _editingController.text;
    widget.onTapComment(text);
    _editingController.text = '';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 90.h,
      padding: EdgeInsets.fromLTRB(20.w, 0, 20.w, 0),
      color: Color(0xFFF2F2F2),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Expanded(
            child: Row(
              children: <Widget>[
                Text(
                  widget.hintText,
                  style: TextStyle(
                    fontSize: 28.sp,
                    color: Colors.black87,
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 70.h,
                    color: Colors.white,
                    child: TextField(
                      controller: _editingController,
                      focusNode: widget.commentFocus,
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: Colors.black87,
                      ),
                      maxLength: 140,
                      minLines: 1,
                      maxLines: 9,
                      keyboardType: TextInputType.multiline,
                      textInputAction: TextInputAction.send,
                      onEditingComplete: sendComment,
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.fromLTRB(10.w, 0, 10.w, 7.h),
                        border: InputBorder.none,
                        counterText: '',
                        hintStyle: TextStyle(
                          fontSize: 28.sp,
                          color: Colors.black87,
                        ),
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: sendComment,
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(4.w),
                      color: _isClick ? Color(0xFF0CB7D9) : Colors.grey,
                    ),
                    margin: EdgeInsets.only(left: 10),
                    alignment: Alignment.center,
                    width: 100.w,
                    height: 70.h,
                    child: Text(
                      widget.btnText,
                      style: TextStyle(
                        fontSize: 30.sp,
                        color: Color(0xFFFFFFFF),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
